﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="TestWebRequestStream.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace AzureManagementTests.TestSupport
{
    using System;
    using System.IO;
    using System.Text;

    public class TestWebRequestStream : Stream
    {
        private byte[] content;

        public override long Position { get; set; }

        public override bool CanRead
        {
            get
            {
                return true;
            }
        }

        public override bool CanSeek
        {
            get
            {
                return true;
            }
        }

        public override bool CanWrite
        {
            get
            {
                return true;
            }
        }

        public override long Length
        {
            get
            {
                return this.content.Length;
            }
        }

        public override void Flush()
        {
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return offset;
        }

        public override void SetLength(long value)
        {
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            this.content.CopyTo(buffer, offset);

            return Math.Min(buffer.Length, this.content.Length);
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            this.content = buffer;
        }

        public string GetString()
        {
            if (this.content == null)
            {
                return string.Empty;
            }

            return new UTF8Encoding().GetString(this.content);
        }
    }
}